<template>
  <div class="modify-dialog">
    <slot>
      <el-button size="small" type="primary" @click="showDialog()">
        新增采购单
      </el-button>
    </slot>
    <el-dialog v-bind="dialogOpts" :visible.sync="dialogVisible">
      <Form v-bind="formOpts" v-on="formEvent">
        <template #formSuf v-if="modify == 'add'">
          <el-form-item label="材料" style="width: 100%">
            <ProductInput
              mode="supplierId"
              :params="{ supplierId: form.supplierId.value, pageSize: 10000,pageNumber:1 }"
              v-model="arr"
            />
          </el-form-item>
        </template>
        <template #formBtn>
          <div class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="onConfirm"> 确 定 </el-button>
          </div>
        </template>
      </Form>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import ProductInput from "./ProductInput.vue";
import modifyMixins from "@/mixins/modifyMixins.vue";
export default {
  mixins: [modifyMixins],
  components: {
    ProductInput,
  },
  data() {
    return {
      dialogWidth: "600px",
      form: {
        orderName: {
          tag: "el-input",
          label: "订单名称",
          value: "",
          props: {
            clearable: true,
            filterable: true,
            placeholder: "请输入订单名称",
          },
        },
        advanceCharge: {
          tag: "el-input-number",
          label: "预付款",
          value: "",
          props: {
            min: 0,
            precision: 2,
            clearable: true,
            placeholder: "请输入预付款",
          },
        },
        procureVouchers: {
          tag: "Uploader",
          label: "下单单据",
          value: [],
          props: {},
        },
        // arrivalVouchers: {
        //   tag: "Uploader",
        //   label: "到货凭证",
        //   value: [],
        //   props: {},
        // },
        // paymentVouchers: {
        //   tag: "Uploader",
        //   label: "付款单据",
        //   value: [],
        //   props: {},
        // },
        remark: {
          tag: "el-input",
          label: "备注",
          value: "",
          props: {
            rows: 3,
            type: "textarea",
            clearable: true,
            placeholder: "请输入备注",
          },
        },
        warehouseId: {
          tag: "el-select",
          label: "仓库",
          value: "",
          items: [],
          props: {
            clearable: true,
            filterable: true,
          },
        },
        supplierId: {
          tag: "el-select",
          label: "供应商",
          value: "",
          items: [],
          props: {
            clearable: true,
            filterable: true,
          },
        },
      },
    };
  },
  computed: {},
  watch: {
    "form.supplierId.value": function () {
      this.arr = [{ id: "", count: 0, key: this.$quinn.guid() }];
    },
  },
  mounted() {},
  methods: {
    showDialog(modify = "add", row = {}) {
      this.dialogVisible = true;
      this.row = row;
      this.modify = modify || "add";
      let keys = ["warehouseId", "supplierId"];
      this.form.warehouseId.items = this.$store.getters.warehouseListItems;
      this.form.supplierId.items = this.$store.getters.supplierListItems;
      if (modify == "editProcureVouchers")
        keys = keys.concat(["advanceCharge", "remark"]);
      for (const key in this.form) {
        let defValue = "";
        if (Array.isArray(this.form[key].value)) defValue = [];
        if (Object.hasOwnProperty.call(row, key))
          this.form[key].value = row[key] || defValue;
        else this.form[key].value = defValue;
        this.form[key].hidden = false;
        if (keys.includes(key))
          this.form[key].hidden = ["edit", "editProcureVouchers"].includes(
            modify
          );
      }
      this.formKey = this.$quinn.guid();
    },
    onSubmit(form) {
      const { modify, row } = this;
      form.procureVouchers = form.procureVouchers.join(",");
      // form.arrivalVouchers = form.arrivalVouchers.join(",");
      // form.paymentVouchers = form.paymentVouchers.join(",");
      if (modify == "add")
        form.materials = this.arr.map((e) => ({ count: e.count, id: e.id }));
      if (modify === "add")
        this.$api.procureProcureAdd(form).then(this.submitDoneMsg);
      else if (["edit", "editProcureVouchers"].includes(modify))
        this.$api
          .procureProcureUpdate({ ...form, procureId: row.procureId })
          .then(this.submitDoneMsg);
    },
  },
};
</script>

<style lang="less" scoped></style>
